package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.security.CurrentPrincipal;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.repository.IMemberRepository;
import cn.tedu.flower.manage.content.pojo.entity.Goods;
import cn.tedu.flower.manage.content.pojo.entity.Member;
import cn.tedu.flower.manage.content.pojo.param.FadebackAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.MemberAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.MemberUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.GoodsStandardVO;
import cn.tedu.flower.manage.content.pojo.vo.MemberListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.MemberStandardVO;
import cn.tedu.flower.manage.content.service.IMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class MemberServiceImpl implements IMemberService {

    @Value("${flower-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IMemberRepository memberRepository;

    public MemberServiceImpl(){
        log.info("创建业务对象：MemberServiceImpl");
    }

    @Override
    public void addNew(CurrentPrincipal currentPrincipal, String remoteAddr, MemberAddNewParam memberAddNewParam) {
        log.debug("开始处理【添加会员】的业务，当事人：{}，IP地址：{}，参数：{}", currentPrincipal, remoteAddr, memberAddNewParam);
        MemberStandardVO memberStandardVO = memberRepository.selectByUsername(memberAddNewParam.getUsername());

        if (memberStandardVO != null) {
            String message = "添加会员失败,该会员已存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        Member member = new Member();
        BeanUtils.copyProperties(memberAddNewParam, member);
        int rows = memberRepository.insert(member);
        if (rows != 1) {
            String message = "添加会员失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }
    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除会员】的业务，参数：{}", id);
        MemberStandardVO memberId = memberRepository.selectById(id);
        if (memberId==null) {
            String message="删除会员失败,尝试删除的会员数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        int rows = memberRepository.deleteById(id);
        if (rows!=1) {
            String message = "删除会员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(MemberUpdateParam memberUpdateParam) {
        log.debug("开始处理【修改会员】的业务,参数:{}",memberUpdateParam);
        MemberStandardVO standardByNickname = memberRepository.selectByNickname(memberUpdateParam.getNickname());
        if (standardByNickname==null) {
            String message="修改会员信息失败,该会员数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        Member member=new Member();
        BeanUtils.copyProperties(member,memberUpdateParam);
        int rows = memberRepository.update(member);
        if (rows!=1) {
            String message="修改会员信息失败,服务器繁忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void setStatusOn(Long id) {
        log.debug("开始处理【会员启用】的业务，参数：{}", id);
        updateStatusById(id,1);
    }

    @Override
    public void setStatusOff(Long id) {
        log.debug("开始处理【会员禁用】的业务，参数：{}", id);
        updateStatusById(id,0);
    }

    @Override
    public MemberStandardVO selectById(Long id) {
        MemberStandardVO memberVO = memberRepository.selectById(id);
        if (memberVO==null) {
            String message="查询会员详情失败,该会员不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return memberVO;
    }

    @Override
    public MemberStandardVO selectByUsername(String username) {
        MemberStandardVO memberVO = memberRepository.selectByUsername(username);
        if (memberVO==null) {
            String message="查询会员详情失败,该会员不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return memberVO;
    }

    @Override
    public MemberStandardVO selectByNickname(String nickname) {
        MemberStandardVO memberVO = memberRepository.selectByNickname(nickname);
        if (memberVO==null) {
            String message="查询会员详情失败,该会员不存在";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return memberVO;
    }

    @Override
    public PageData<MemberListItemVO> selectAll(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询会员列表】的业务，页码：{}", pageNum);
        PageData<MemberListItemVO> pageData = memberRepository.selectAll(pageNum, pageSize);
        return pageData;
    }

    @Override
    public PageData<MemberListItemVO> selectAll(Integer pageNum) {
        log.debug("开始处理【查询会员列表】的业务，页码：{}", pageNum);
        PageData<MemberListItemVO> pageData = memberRepository.selectAll(pageNum, defaultQueryPageSize);
        return pageData;
    }

    private void updateStatusById(Long id,Integer status){
        MemberStandardVO memberStandardVO = memberRepository.selectById(id);
        if (memberStandardVO==null) {
            String message="将会员状态修改为"+STATUS_TEXT[status]+"失败,会员数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        if (memberStandardVO.getState() == status) {
            String message="将会员状态修改为"+STATUS_TEXT[status]+"失败,该会员已"+STATUS_TEXT[status];
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        Member updateMember=new Member();
        updateMember.setId(id);
        updateMember.setState(status);
        int rows = memberRepository.update(updateMember);
        if (rows!=1) {
            String message="将会员状态修改为"+STATUS_TEXT[status]+"失败,服务器繁忙,请稍后后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }

    }
}
